package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

public abstract class UserContext {

    public static final String EMPLOYEE_IN_SESSION = "EMPLOYEE_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION = "EXPRESSION_IN_SESSION";


    //获取session对象

    /**
     * 使用springmvc  提供的工具类,获取session对象
     *
     * @return
     */
    public static HttpSession getSession () {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attrs.getRequest().getSession();
    }


    //设置登录用户存入session
    public static void setCurrentUser (Employee emp) {
        getSession().setAttribute(EMPLOYEE_IN_SESSION, emp);
    }


    //获取当前登录用户
    public static Employee getCurrentUser () {
        return (Employee) getSession().getAttribute(EMPLOYEE_IN_SESSION);
    }


    //设置登录用户的权限信息存入session
    public static void setExpression (List<String> permissions) {
        getSession().setAttribute(EXPRESSION_IN_SESSION, permissions);
    }


    //从session获取登录用户的权限信息
    public static List<String> getExpression () {
        return (List<String>) getSession().getAttribute(EXPRESSION_IN_SESSION);
    }
}
